<?php
/**
 * 班级信息接口流水
 */
namespace App\Http\Controllers\Flow;


use App\Exceptions\APIHttpException;
use App\Http\Controllers\Controller;
use App\Http\Library\Excel;
use App\Http\Middleware\Auth;
use App\Http\Model\Flow\ClassListHistoryModel;
use App\Http\ModelDao\Flow\ClassFlowDao;
use App\Http\Library\Common;
use App\Enum\ClassListHistoryEnum;



class ClassFlow extends Controller
{
    /**
     * 班级信息接口流水列表
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function classFlow()
    {
        $where = $this->_combineSearchCondition();
        $page = (int)request()->input('page', 1);
        $pageSize = Common::limit( (int)request()->input('page_size', 30) );

        $businessUnitID = (int) request()->input('business_unit_id');
        $ids = explode(',', Auth::pfs([$businessUnitID]));

        $enablePage = true; //是否启用分页查询
        $classFlowData = ClassListHistoryModel::fetchClassFlowData($ids, $where, $enablePage, $page, $pageSize);

        $classFlowCnt = ClassListHistoryModel::fetchClassFlowCnt($ids, $where);

        //班级信息接口流水数据处理
        $ret = ClassFlowDao::classFlowList($classFlowData);

        $result = ['data' => $ret, 'start' => ($page-1) * $pageSize + 1, 'page' => $page,
            'page_size' => $pageSize, 'total' => $classFlowCnt, //'baseUrl' => URL::full()
        ];
        return APIHttpException::success($result);
    }

    /**
     * 导出班级信息接口流水
     *
     * @throws \Exception
     */
    public function exportFlowData()
    {
        $where = $this->_combineSearchCondition();

        $businessUnitID = (int) request()->input('business_unit_id');
        $ids = explode(',', Auth::pfs([$businessUnitID]));

        $enablePage = false; //是否启用分页查询 导出Excel只需要按照搜索条件导出即可
        $classFlowData = ClassListHistoryModel::fetchClassFlowData($ids, $where, $enablePage, 0, 0);

        /*导出的数据不能超过 10万 条
        ($maxLimit = count($classFlowData)) > 100000*/
        if (count($classFlowData) > ClassListHistoryEnum::EXPORT_MAX_LIMIT) {
            throw new APIHttpException(ClassListHistoryEnum::EXPORT_MAX_LIMIT_MSG);
        }
        $flowList = ClassFlowDao::classFlowList($classFlowData);

        $ret = [];
        foreach ($flowList as & $flow) {
            $ret[] = array_values($flow);
        }

        if ($ret) {
            $fileName = date('YmdHis') . '_班级信息接口流水.xlsx';

            $classFlowTitle = ['推送日期', '城市', '主体名称', '班级ID', '班级名称',
                '开课日期', '结课日期', '课程类型', '推送状态', '描述信息'];

            Excel::$download = true;
            Excel::$title = $classFlowTitle;
            Excel::send($ret, $fileName);
            exit;
        }
    }

    private function _combineSearchCondition()
    {
        $startTime = trim(request()->input('start_time'));
        $endTime = trim(request()->input('end_time'));
        $className = trim(request()->input('class_name'));
        $pushStatus = trim(request()->input('push_status'));
        $cityName = trim(request()->input('city_name'));
        $mainName = trim(request()->input('main_name')); //主体名称

        $where = [];
        if ($className) {
            $where['class_name'] = $className;
        }
        if ($pushStatus) {
            $where['result'] = $pushStatus;
        }
        if ($cityName) {
            $where['city_name'] = $cityName;
        }
        if ($mainName) {
            $where['ledger_subject_name'] = $mainName;
        }

        if(!empty($startTime)) {
            $where[] = ['push_time', '>=', dateFrom(urldecode($startTime))];
        }
        if (!empty($endTime)) {
            $where[] = ['push_time', '<=', dateTo(urldecode($endTime))];
        }

        return $where;
    }
}
